package com.example.demo.controller;


import com.example.comon.domain.LoginUser;
import com.example.comon.domain.R;
import com.example.comon.service.TokenService;
import com.example.demo.domain.User;
import com.example.demo.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/login")
@Api(value = "login",tags = "登录控制器")
public class LoginController {

    @Autowired
    private IUserService userService;

    @Autowired
    private TokenService tokenService;

    /*@GetMapping("/login")
    @ApiOperation("登录")
    public R login(User user){
        User user1 = new User();
        user1.setAccount(user.getAccount());
        User oneUser =  userService.selectOneUser(user1);
        if(oneUser == null){
            return R.error("账号不存在，请检查账号是否正确");
        }
        if(!oneUser.getPassword().equals(user.getPassword())){
            return R.error("密码不正确，请重新输入密码");
        }
        //模拟令牌 动态的
        String token=oneUser.getAccount()+"@@@"+oneUser.getPassword();
        return R.success("登录成功",token);
    }*/

    @GetMapping("/login")
    @ApiOperation("登录")
    public R login(User user){
        User user1 = new User();
        user1.setAccount(user.getAccount());
        User oneUser =  userService.selectOneUser(user1);
        if(oneUser == null){
            return R.error("账号不存在，请检查账号是否正确");
        }
        if(!oneUser.getPassword().equals(user.getPassword())){
            return R.error("密码不正确，请重新输入密码");
        }
        LoginUser loginUser = new LoginUser();
        loginUser.setUserId(oneUser.getId());
        String token= tokenService.createToken(loginUser);
        return R.success("登录成功",token);
    }
}
